package com.example.administrator.officeplatform.kj;

import com.example.administrator.officeplatform.MyApplication;
import com.example.administrator.officeplatform.bean.SpinnerInfo;
import com.example.administrator.officeplatform.bean.TimeInfo;
import com.example.administrator.officeplatform.utils.JsonUtil;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Administrator on 2017/12/20.
 */

public class TimeSearch {
    private static String number;
    private static List<TimeInfo> ls_audit = new ArrayList<>();
    private static ArrayList ls = new ArrayList();
    public static ArrayList getTime(){
        OkHttpClient mOkHttpClient = new OkHttpClient();
        final Request request = new Request.Builder()
                .url("http://192.168.5.244:8871/reimbursement/setlecvcle/setlecycleList")
                .header("cookie", MyApplication.getMsession())
                .build();
        Call call = mOkHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                number="连接异常";
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //{"error":"您不属于事业部下,无法生成报销单号"}
                //{"number":"henfanachaojifana2017121900004"}
                String str = response.body().string();
                if(str.indexOf("error")>=0){
                    String Msg = JsonUtil.getMsg(str,"error");
                }else {
                    final List<TimeInfo> lsMap = JsonUtil.getPersons(str, new TypeToken<List<TimeInfo>>() {
                    }.getType());
                    ls_audit.addAll(lsMap);
                     ls = new ArrayList();
                    for (int i = 0; i < lsMap.size(); i++) {
                        String id = lsMap.get(i).getId();
                        String text = lsMap.get(i).getText();
                        ls.add(new SpinnerInfo(id, text));
                    }

                }
            }
        });

        return ls;
    }
}
